/**
 * This file is part of Wireless TransFer.
 *
 *   Wireless TransFer is free software: you can redistribute it and/or modify
 *   it under the terms of the GNU General Public License as published by
 *   the Free Software Foundation, either version 3 of the License, or
 *   (at your option) any later version.
 *	
 *   Wireless TransFer is distributed in the hope that it will be useful,
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *   GNU General Public License for more details.
 *
 *   You should have received a copy of the GNU General Public License
 *   along with Wireless TransFer.  If not, see <http://www.gnu.org/licenses/>.
 *
 *	 Copyright (C) 2011
 *   @author prebz
 */

package com.wirelesstransfer.activity.receive;

import com.wirelesstransfer.Main;
import com.wirelesstransfer.R;
import com.wirelesstransfer.activity.dialogs.Question;
import com.wirelesstransfer.client.ApacheClient;
import com.wirelesstransfer.client.ClientConnection;
import com.wirelesstransfer.server.WIFI.JettyServer;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.Dialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.res.Resources;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.EditText;

public class Receive extends Activity implements OnClickListener{

	private EditText ipEditText;
	private Authenticate authenticate;
	private String ipAdress;

	private Resources res;
	
	public static final int DIALOG_ERROR = 0;

	private ApacheClient client;
	private JettyServer server;
	private ClientConnection clientConnection;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.receive);

		res = getResources();
		
		
		// Click listener for button
		View connectButton = findViewById(R.id.connect_button);
		connectButton.setOnClickListener(this);

		// Click listener for textField
		ipEditText = (EditText) findViewById(R.id.ip_edit);
		ipEditText.setOnClickListener(this);

		authenticate = new Authenticate();

		ipAdress = null;

	}
	@Override
	public void onResume(){
		super.onResume();
		
		client = ApacheClient.getInstance();
		server = JettyServer.getInstance();
		
		Log.v("status", "client is active"  + client.isActiveClient());
		if(server.isRunning() || 
			client.isActiveClient()){
			
			buildInitDialog(res.getString(R.string.receive_dialog_init).toString()).show();
			
		}
	}

	
	public void onClick(View v) {
		switch(v.getId()){
		case R.id.connect_button:
			authenticateIp();
			break;
		}

	}

	@Override
	protected Dialog onCreateDialog (int id){
		Dialog dialog = null;	
		switch (id) {
		case DIALOG_ERROR:
			dialog = buildOKDialog(res.getString(R.string.receive_NOT_VALID_IP));
			break;
		default:
			dialog = null;
			break;
		}
		return dialog;

	}

	/**
	 * get the value in the code field
	 * @return true if valid
	 */
	private boolean authenticateIp(){
				
		ipAdress = authenticate.getValueIp(ipEditText.getText().toString());
		//ipAdress = "10.0.2.2";
		if (ipAdress != null){
			client.connect(authenticate.getValueIp(ipEditText.getText().toString()));

	// remove when done testing and uncomment above and bellow
//			client.connect(ipAdress);
//			client.authenticate(111222);
//     	   client.selectFiles();
//    	   client.receiveFiles();	


			
			Intent codeAuthentication = new Intent(this, CodeAuthentication.class);
			startActivity(codeAuthentication);
			return true;
		} else {
			showDialog(DIALOG_ERROR);
			return false;
		}
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		MenuInflater inflater = getMenuInflater();
		inflater.inflate(R.menu.questionmenu, menu);
		return true;
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		switch (item.getItemId()) {
		case R.id.question_menu:
			Intent question = new Intent(this, Question.class);
			question.putExtra(res.getString(R.string.question_TITLE),res.getString(R.string.question_receive_title_txt));
			question.putExtra(res.getString(R.string.question_TEXT),res.getString(R.string.question_receive_txt));
			startActivity(question);
			return true;
		default:
			return super.onOptionsItemSelected(item);
		}
	}

	/**
	 * Creates text in a dialog with Ok key
	 * @param action
	 * @return
	 */
	private AlertDialog buildOKDialog(String action){
		AlertDialog.Builder builder = new AlertDialog.Builder(this);
		builder.setMessage(action)
		.setCancelable(false)

		.setNegativeButton("Ok", new DialogInterface.OnClickListener() {
			public void onClick(DialogInterface dialog, int id) {
				dialog.cancel();
			}
		});
		AlertDialog alert = builder.create();
		return alert;
	}
	/**
	 * Switch to a specific tab
	 * @param indexTabToSwitchTo
	 */
	private void switchTabInActivity(int indexTabToSwitchTo){
        Main ParentActivity;
        ParentActivity = (Main) this.getParent();
        ParentActivity.switchTab(indexTabToSwitchTo);
	}

	/**
	 * Creates text in a dialog with yes/no key
	 * @param action
	 * @return
	 */
	public AlertDialog buildInitDialog(String action){
		AlertDialog.Builder builder = new AlertDialog.Builder(this);
		builder.setMessage(action)
		.setCancelable(false)
		.setPositiveButton(res.getString(R.string.send_dialog_yes), new DialogInterface.OnClickListener() {
			public void onClick(DialogInterface dialog, int id) {
				client.disconnect(clientConnection.getSessionId());
			}
		})
		.setNegativeButton(res.getString(R.string.send_dialog_no), new DialogInterface.OnClickListener() {
			public void onClick(DialogInterface dialog, int id) {
				switchTabInActivity(0); 	
			}
		});
		AlertDialog alert = builder.create();
		return alert;
	}

}
